package com.clkj.cldisabledmanclient.mvp.myrecovertrain;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.clkj.cldisabledmanclient.R;
import com.clkj.cldisabledmanclient.adapter.PhotoAdapter;
import com.clkj.cldisabledmanclient.app.CustomApplication;
import com.clkj.cldisabledmanclient.base.BaseActivity;
import com.clkj.cldisabledmanclient.bean.MyRecoverTrainDetail;
import com.clkj.cldisabledmanclient.bean.PersonInfo;
import com.clkj.cldisabledmanclient.constant.ShareKey;
import com.clkj.cldisabledmanclient.util.CommonUtil;
import com.clkj.cldisabledmanclient.util.ToastUtil;
import com.clkj.cldisabledmanclient.util.scheduler.SchedulerProvider;

import java.util.ArrayList;
import java.util.List;

public class MyRecoverTrainDetailActivity extends BaseActivity implements MyRecoverTrainDetailContract.View {


    private RelativeLayout mRlTitle;
    private ImageView mIvBack;
    private TextView mTvTitle;
    private TextView mTvRight;
    private ImageView mIvAvator;
    private TextView mTvName;
    private TextView mTvSex;
    private TextView mTvBirthday;
    private TextView mTvCanjiType;
    //private TextView mTvCanjilevel;
    private RecyclerView mRvPhotos;
    private TextView mTvTrainDate;

    private MyRecoverTrainDetail myRecoverTrainDetail;

    private MyRecoverTrainDetailContract.Presenter mPresenter;
    private ProgressDialog progressDialog;
    private String progressId;
    private String idCard;

    private List<String> photos = new ArrayList<>();

    private PhotoAdapter photoAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myrecovertrain_detail);
        initTitle(null, null, "我的康复记录详情", true, null);
        initData();
        initView();
        assignView();

        getPersonInfo();
        getMyRecoverTrainDetail();
    }

    @Override
    public void initData() {

        idCard = (String) CommonUtil.getShareValue(ShareKey.USER_IDCARD, "");
        progressId = getIntent().getStringExtra("progressId");
        mPresenter = new MyRecoverTrainDetailPresenter(this, SchedulerProvider.getInstance(), CustomApplication.getInstance().getHttpService());

    }

    @Override
    public void assignView() {

        //LinearLayoutManager llManager = new LinearLayoutManager(this);
        GridLayoutManager llManager = new GridLayoutManager(this, 3);
        llManager.setOrientation(LinearLayoutManager.VERTICAL);
        //解决scrollview和recycleview嵌套滑动冲突
        llManager.setSmoothScrollbarEnabled(true);
        llManager.setAutoMeasureEnabled(true);

        mRvPhotos.setLayoutManager(llManager);
        mRvPhotos.setNestedScrollingEnabled(false);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        mRvPhotos.setHasFixedSize(true);

        photoAdapter = new PhotoAdapter(photos, this);
        mRvPhotos.setAdapter(photoAdapter);

        /*if (postModel.getImgUrl().size()!=0) {
            int num = postModel.getImgUrl().size();
            photos.clear();
            for (int i = 0; i < num; i++) {
                photos.add(postModel.getImgUrl().get(i));
            }

            PhotoAdapter.notifyDataSetChanged();
        }*/

        //获取焦点，防止被recycleview抢占
        mTvName.setFocusable(true);
        mTvName.setFocusableInTouchMode(true);
        mTvName.requestFocus();
        mRvPhotos.setFocusable(false);
    }

    @Override
    public void initView() {


        mRlTitle = findViewById(R.id.rl_title);
        mIvBack = findViewById(R.id.iv_back);
        mTvTitle = findViewById(R.id.tv_title);
        mTvRight = findViewById(R.id.tv_right);
        mIvAvator = findViewById(R.id.iv_avator);
        mTvName = findViewById(R.id.tv_name);
        mTvSex = findViewById(R.id.tv_sex);
        mTvBirthday = findViewById(R.id.tv_birthday);
        mTvCanjiType = findViewById(R.id.tv_canjiType);
        mTvTrainDate = findViewById(R.id.tv_trainDate);
        // mTvCanjilevel = findViewById(R.id.tv_canjilevel);
        mRvPhotos = findViewById(R.id.rv_photos);
    }

    @Override
    public void getPersonInfo() {

        mPresenter.getPersonInfo(idCard);
    }

    @Override
    public void getMyRecoverTrainDetail() {

        mPresenter.getMyRecoverTrainDetail(progressId);
    }

    @Override
    public void getMyRecoverTrainDetailSuccess(MyRecoverTrainDetail myRecoverTrainDetail) {

        this.myRecoverTrainDetail = myRecoverTrainDetail;

        mTvTrainDate.setText(myRecoverTrainDetail.getStartTime() + "至" + myRecoverTrainDetail.getEndTime());
        photos.addAll(myRecoverTrainDetail.getFilePath());
        photoAdapter.notifyDataSetChanged();
    }

    @Override
    public void getMyRecoverTrainDetailError(String tip) {
        ToastUtil.showShort(this, tip);
    }

    @Override
    public void getPersonInfoSuccess(PersonInfo personInfo) {

        //获取到即往视图里面插数据
        mTvName.setText("姓名：" + personInfo.getName());
        mTvSex.setText("性别：" + personInfo.getGender());
        mTvBirthday.setText("出生年月：" + personInfo.getBirthday());
        //mTvCanjilevel.setText(personInfo.getDisabilityCategory());
        mTvCanjiType.setText(personInfo.getDisabilityCategory());
    }

    @Override
    public void getPersonInfoError(String tip) {
        ToastUtil.showShort(this, tip);
    }

    @Override
    public void setPresenter(MyRecoverTrainDetailContract.Presenter presenter) {

    }


    @Override
    public void showLoading() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setIndeterminate(false);
        progressDialog.setCancelable(true);
        //设置点击进度对话框外的区域对话框不消失
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.setMessage("正在加载...");
        progressDialog.show();
    }

    @Override
    public void dismissLoading() {
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
    }
}
